package com.admin.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.admin.bean.BillingSpBean;
import com.admin.bean.BillingSpVo;
import com.admin.bean.SpBean;
import com.admin.cache.EhCacheManager;
import com.admin.dao.BillingSpDao;
import com.admin.dao.FeeBackDao;
import com.admin.dao.SpDao;

@Service
public class SpService {

	@Autowired private SpDao spDao;
	@Autowired private BillingSpDao billingSpDao;
	@Autowired private FeeBackDao feeBackDao;
	
	public int getConsume(String phone,String command){
		return this.feeBackDao.getConsume(phone, command);
	}
	
	/**
	 * 根据条件查询SP数
	 * @param provinceId
	 * @param isp
	 * @param vodCustomized
	 * @return
	 */
	public int findCountByCondition(int provinceId,int isp,int vodCustomized){
		return spDao.findCountByCondition(provinceId, isp, vodCustomized);
	}
	
	/**
	 * 根据条件查询SP列表
	 * @param provinceId
	 * @param isp
	 * @param vodCustomized
	 * @param start
	 * @param pageSize
	 * @return
	 * @throws SQLException
	 */
	public List<SpBean> findSpBeanByCondition(int provinceId,int isp,int vodCustomized,int start,int pageSize) {
		return spDao.findSpBeanByCondition(provinceId, isp, vodCustomized, start, pageSize);
	}
	
	/**
	 * 根据计费点ID查询绑定关系总数
	 * @param billingId
	 * @return
	 */
	public int findCountByBillingId(int billingId){
		return billingSpDao.findCountByBillingId(billingId);
	}
	
	
	/**
	 * 查询该计费点绑定的SP
	 * @param billingId
	 * @return
	 */
	public List<BillingSpVo> findSpByBillingId(int billingId,int start,int pageSize){
		try {
			List<BillingSpBean> spList = billingSpDao.findByBillingId(billingId,start,pageSize);
			List<BillingSpVo> voList = null;
			if(spList!=null && spList.size()>0){
				voList = new ArrayList<BillingSpVo>(spList.size());
				for (BillingSpBean billingSpBean : spList) {
					int spId = billingSpBean.getSpId();
					BillingSpVo vo = new BillingSpVo();
					vo.setBillingSpBean(billingSpBean);
					vo.setSpBean(spDao.findSpBeanById(spId));
					voList.add(vo);
				}
				return voList;
			}else{
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * 解除绑定
	 * @param id
	 * @return
	 */
	public int delBillingSp(int id){
		return billingSpDao.delBillingSp(id);
	}
	

	/**
	 * 绑定SP
	 * @param bsp
	 * @return
	 */
	public int insertBillingSp(BillingSpBean bsp){
		return billingSpDao.insertBillingSp(bsp);
	}
	
	/**
	 * 根据计费点ID 和SPID查询绑定关系
	 * @param spId
	 * @param billingId
	 * @return
	 * @throws SQLException
	 */
	public BillingSpBean findBillingSpBySPIdAndBillingId(int spId,int billingId){
		try {
			return billingSpDao.findBillingSpBySPIdAndBillingId(spId, billingId);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 根据ID查询SP
	 * @param spId
	 * @return
	 */
	public SpBean findSpById(int spId){
		String key = EhCacheManager.SP+spId;
		Element e = EhCacheManager.getValue(key);
		SpBean sp = null;
		if(e==null){
			sp = spDao.findSpBeanById(spId);
			EhCacheManager.putValue(new Element(key, sp));
		}else{
			sp = (SpBean)e.getValue();
		}
		return sp;
	}
	
	
	/**
	 * 添加SP
	 */
	public boolean insertSp(SpBean sp){
		return spDao.addSp(sp);
	}
	
	/**
	 * 修改SP
	 * @param sp
	 * @return
	 */
	public boolean updateSp(SpBean sp){
		if(spDao.updateSp(sp)){
			EhCacheManager.putValue(new Element(EhCacheManager.SP+sp.getSpId(), sp));
			return true;
		}
		return false;
	}
	
	/**
	 * 删除SP
	 * @param spId
	 * @return
	 */
	public boolean delSp(int spId){
		if(spDao.delSp(spId)){
			EhCacheManager.getCache().remove(EhCacheManager.SP+spId);
			billingSpDao.delBillingSpBySpId(spId);
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 
	  * @Title: findSpByPortAndCommand
	  * @Description:根据端口和指令获取SP数 
	  * @param @param port
	  * @param @param command
	  * @param @return
	  * @return int
	  * @throws
	 */
	public int findSpByPortAndCommand(String port,String command){
		return spDao.findSpByPortAndCommand(port, command);
	}
	
	public boolean addSpTest(SpBean sp){
		return spDao.addSpTest(sp);
	}
	
	public List<SpBean> findSpTestBeanList(){
		return spDao.findSpTestBeanList();
	}
	
	public List<SpBean> findSpTestBeanList(int provinceId,int isp){
		return spDao.findSpTestBeanList(provinceId, isp);
	}
	
	public boolean updateSpTestStatus(int spId,int status){
		return spDao.updateSpTestStatus(spId, status);
	}
	
	public boolean deleteSpTestById(int spId){
		return spDao.deleteSpTestById(spId);
	}
}
